package com.hy.handler;

import com.hy.bean.RespBean;
import com.hy.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.*;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    @ResponseBody
    /*处理前端提交数据的异常*/
    public RespBean handleException(BindException e){
//        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
//        List<String> msgList = fieldErrors.stream()
//                .map(o -> o.getDefaultMessage())
//                .collect(Collectors.toList());
//        String messages = StringUtils.join(msgList.toArray(), ";;;");

        return new RespBean().error("前端数据异常",e.getMessage());
    }

    @ExceptionHandler(BizException.class)
    @ResponseBody
    public RespBean handleException(BizException e){
        //给后端人员看的
        log.debug("{}->{},",e.getMessage(),e.getCause().getMessage());

        //返回给前端人员看的
        return new RespBean().error("后台异常报错",e.getMessage());
    }

//    @ExceptionHandler(TokenExpiredException.class)
//    @ResponseBody
//    public RespBean handleException(TokenExpiredException e){
//        System.out.println(e);
//        //给后端人员看的
//        log.debug("{}->{},",e.getMessage());
//        //返回给前端人员看的
//        return new RespBean().error("亲亲，请登录",e.getMessage());
//    }

}
